<?php
namespace app\admin\controller;
use app\common\controller\Base;
use app\admin\model\User as userModel;
use app\common\validate\User as validateUser;
use think\facade\Cookie;
use think\facade\Request;
use think\facade\Session;

class User extends Base{
    public $expireTime = 14400; //用户信息过期时间设置为4小时
    public $cookieExpireTime = 3600 * 24 * 15; //用户cookie过期时间设置为15天
    /**
     * @return \think\response\View
     */
    public function logion(){
        return view();
    }
    /**
     * 管理员登录
     */
    public function logions(){

        $session_admin = parent::getUserInfo(1);
        if ($session_admin != false){
            $this->redirect('admin/index/index');
        }
        $userData = Request::post();
        if(empty($userData)){
            $validateUser = new validateUser();
            if (!$validateUser->scene('login')->check($userData)) {
//                $this->error($validateUser->getError(), 'admin/index/index', '', 1);
                $this->redirect('admin/user/logion');
            }
        }
        $userinfo = new userModel();
        $result = $userinfo->checkUserinfo($userData['username'], $userData['password']);
        if (false === $result){
            $this->error('没有找到用户信息,用户名或密码错误','admin/user/logion');
        }
        $result->uuid = $userinfo->getUuid();
        $result->token_expire_time = date('Y-m-d H:i:s',time()+3600*3);
        $result['expire_time'] = time() + $this->expireTime;
        Session::set('userinfo', $result);
        Cookie::set('userinfo',json_encode($result),$this->cookieExpireTime);
        $this->redirect('admin/index/index');
    }
    /*

     */
    /**
     * 退出登录
     *
     * @return void
     */
    public function loginOut()
    {
        userModel::clearUserinfo();
        $this->redirect('admin/user/logion');
    }
}